package com.jyw.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.jyw.common.exception.ValidationException;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.TimeOverlapUtil;
import com.jyw.system.domain.SysConfig;
import com.jyw.system.domain.vo.SysOrderExpectedTimeConfigVo;
import com.jyw.system.enums.OrderExpectedTimeEnum;
import com.jyw.system.enums.OtherConfigEnum;
import com.jyw.system.service.ISysConfigService;
import com.jyw.system.service.ISysOrderExpectedTimeConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author Created by ZhangCheng on 2024/01/11
 */
@Service
public class SysOrderExpectedTimeConfigServiceImpl implements ISysOrderExpectedTimeConfigService {

    @Resource
    private ISysConfigService sysConfigService;

    /**
     * 超时完成时间默认4小时
     */
    private static final int TIMEOUT_HOUR=4;

    @Override
    public SysOrderExpectedTimeConfigVo getConfig() {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.EXPECTED_TIME.getKey());
        if (sysConfig != null && StrUtil.isNotEmpty(sysConfig.getConfigValue())) {
            return JSONObject.parseObject(sysConfig.getConfigValue(), SysOrderExpectedTimeConfigVo.class);
        }
        return null;
    }

    @Override
    public SysOrderExpectedTimeConfigVo getDefaultConfig() {
        SysOrderExpectedTimeConfigVo configVo=getConfig();
        if(configVo!=null){
            return configVo;
        }
        SysOrderExpectedTimeConfigVo configVo1=new SysOrderExpectedTimeConfigVo();
        configVo1.setTimeoutHour(TIMEOUT_HOUR);
        return configVo1;
    }

    @Override
    public void updateConfig(SysOrderExpectedTimeConfigVo configVo) {
        LocalTime time1Start = configVo.getMorningStartTime();
        LocalTime time1End = configVo.getMorningEndTime();
        LocalTime time2Start = configVo.getAfternoonStartTime();
        LocalTime time2End =configVo.getAfternoonEndTime();
        LocalTime time3Start = configVo.getTomorrowStartTime();
        LocalTime time4End =configVo.getTomorrowEndTime();
        boolean overlap = TimeOverlapUtil.checkLocalTime(time1Start, time1End, time2Start, time2End);
        if(overlap){
            throw new ValidationException("上午和下午时间存在重合，请重新设置");
        }
        boolean overlap1 = TimeOverlapUtil.checkLocalTime(time1Start, time2End, time3Start, time4End);
        if(overlap1){
            throw new ValidationException("上午或下午和明日时间存在重合，请重新设置");
        }
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.EXPECTED_TIME.getKey());
        String configValue = JSONObject.toJSONString(configVo);
        if (sysConfig != null) {
            sysConfig.setConfigValue(configValue);
            sysConfig.setUpdateTime(DateUtils.getNowDate());
            sysConfigService.updateConfig(sysConfig);
        } else {
            sysConfigService.initConfig(configValue,OtherConfigEnum.EXPECTED_TIME);
        }
    }

    @Override
    public Integer getExpectedTimeType(LocalDateTime localDateTime) {
        SysOrderExpectedTimeConfigVo configVo=getConfig();
        if(configVo!=null){
            LocalDate localDate=LocalDate.now();
            if(configVo.getMorningStartTime()!=null&&configVo.getMorningEndTime()!=null){
                LocalDateTime startTime=localDate.atTime(configVo.getMorningStartTime());
                LocalDateTime endTime=localDate.atTime(configVo.getMorningEndTime());
                boolean bool=filterTime(startTime,endTime,localDateTime);
                if(bool){
                    return OrderExpectedTimeEnum.MORNING.getValue();
                }
            }
            if(configVo.getAfternoonStartTime()!=null&&configVo.getAfternoonEndTime()!=null){
                LocalDateTime startTime=localDate.atTime(configVo.getAfternoonStartTime());
                LocalDateTime endTime=localDate.atTime(configVo.getAfternoonEndTime());
                boolean bool=filterTime(startTime,endTime,localDateTime);
                if(bool){
                    return OrderExpectedTimeEnum.AFTERNOON.getValue();
                }
            }
            if(configVo.getTomorrowStartTime()!=null&&configVo.getTomorrowEndTime()!=null){
                LocalDateTime startTime=localDate.atTime(configVo.getTomorrowStartTime());
                LocalDateTime endTime=localDate.atTime(configVo.getTomorrowEndTime());
                boolean bool=filterTime(startTime,endTime,localDateTime);
                if(bool){
                    return OrderExpectedTimeEnum.TOMORROW.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 校验时间
     *
     * @param startTime
     * @param endTime
     * @param localDateTime
     * @return
     */
    public boolean filterTime(LocalDateTime startTime, LocalDateTime endTime, LocalDateTime localDateTime) {
        boolean bool = (localDateTime.isAfter(startTime) || localDateTime.isEqual(startTime)) && (localDateTime.isBefore(endTime) || localDateTime.isEqual(endTime));
        return bool;
    }
}
